# -*- coding: utf-8 -*-
from django.db import models
from django.contrib.auth.models import User, Group
from skoolmaster.syllabus.models import FacultyYear, Subject, SchoolYear, UniversityProgram, FacultyModule

class Post(models.Model):
  """
  A numbered post office.
  """
  disabled = models.BooleanField(u'Šifra ni več veljavna', default = False)
  postal_num = models.CharField(u'Poštna številka', max_length = 4, primary_key = True)
  post_office = models.CharField(u'Pošta', max_length = 50)
  
  class Meta:
    ordering = ['post_office']
  
  def __unicode__(self):
    """
    Returns a string representation of this model.
    """
    return u'(%s) %s' % (self.postal_num, self.post_office)

class Region(models.Model):
  """
  Region of a country.
  """
  disabled = models.BooleanField(u'Šifra ni več veljavna', default = False)
  id = models.CharField(max_length = 3, verbose_name = u'Šifra', primary_key = True)
  name = models.CharField(u'Občina', max_length = 50)
  
  class Meta:
    ordering = ['name']
  
  def __unicode__(self):
    """
    Returns a string representation of this model.
    """
    return u'(%s) %s' % (self.id, self.name)

class Country(models.Model):
  """
  A country.
  """
  disabled = models.BooleanField(u'Šifra ni več veljavna', default = False)
  id = models.CharField(max_length = 3, verbose_name = u'Šifra', primary_key = True)
  name = models.CharField(u'Država', max_length = 50)
  
  class Meta:
    ordering = ['name']
  
  def __unicode__(self):
    """
    Returns a string representation of this model.
    """
    return u'(%s) %s' % (self.id, self.name)
  
class Faculty(models.Model):
  """
  A faculty.
  """
  disabled = models.BooleanField(u'Šifra ni več veljavna', default = False)
  id = models.CharField(max_length = 3, verbose_name = u'Šifra', primary_key = True)
  name = models.CharField('Fakulteta', max_length = 50)
  
  class Meta:
    ordering = ['name']
  
  def __unicode__(self):
    """
    Returns a string representation of this model.
    """
    return u'(%s) %s' % (self.id, self.name)

class UserProfile(models.Model):
  """
  General user profile containing additional user attributes.
  """
  disabled = models.BooleanField(u'Šifra ni več veljavna', default = False)
  user = models.ForeignKey(User, unique = True, primary_key = True, verbose_name = u'Uporabnik')
  subjects = models.ManyToManyField(Subject, related_name = 'students', verbose_name = u'Predmeti', blank = True)  
  sifra = models.CharField(u'Šifra', max_length = 6, unique = True, blank = True, null = True)
  
  # Basic student info
  student_number = models.CharField(u'Vpisna številka', max_length = 8, unique = True, null = True, blank = True)
  maiden_name = models.CharField(u'Dekliško ime', max_length = 50, blank = True)
  surname = models.CharField(u'Priimek', max_length = 50)
  name = models.CharField(u'Ime', max_length = 50)
  identity_num = models.CharField(u'EMŠO', max_length = 13)
  fiscal_code = models.CharField(u'Davčna številka', max_length = 8)
  SPECIAL_NEEDS = (
    ('A', u'slepi/slabovidni'),
    ('B', u'gluhi/naglušni'),
    ('C', u'gibalno ovirani/invalidi'),
    ('D', u'drugo')
  )
  special_needs = models.CharField(u'Posebne potrebe', max_length = 1, choices = SPECIAL_NEEDS, null = True, blank = True)

  # Permanent residence info
  address = models.CharField(u'Naslov', max_length = 128)
  post = models.ForeignKey(Post, related_name = 'permanent_users', verbose_name = u'Pošta', null = True)
  region = models.ForeignKey(Region, related_name = 'permanent_users', verbose_name = u'Občina', null = True)
  country = models.ForeignKey(Country, verbose_name = u'Država', null = True)
  phone_num = models.CharField(u'Telefonska številka', max_length = 50, null = True)
  mobile_num = models.CharField(u'Mobilni telefon', max_length = 50, null = True)
  email = models.EmailField(u'Elektronska pošta', null = True)
  
  # Temporary residence info
  tmp_address = models.CharField(max_length = 128, verbose_name = u'Začasen naslov', null = True, blank = True)
  tmp_post = models.ForeignKey(Post, related_name = 'temp_users', verbose_name = u'Začasna pošta', null = True, blank = True)
  tmp_region = models.ForeignKey(Region, related_name = 'temp_users', verbose_name = u'Začasna občina', null = True, blank = True)
  
  class Meta:
    ordering = ['surname', 'name']
  
  def is_student(self):
    """
    Returns true when this user is a student.
    """
    return Group.objects.get(pk = 1) in self.user.groups.all()
  
  def is_lecturer(self):
    """
    Returns true when this user is a lecturer.
    """
    return Group.objects.get(pk = 2) in self.user.groups.all()
  
  def is_administrator(self):
    """
    Returns true when this user is an administrator.
    """
    return Group.objects.get(pk = 3) in self.user.groups.all()
  
  def is_maintainer(self):
    """
    Returns true when this user is a maintainer.
    """
    return Group.objects.get(pk = 4) in self.user.groups.all()
  
  def get_roles(self):
    """
    Returns a list of roles the current user has.
    """
    roles = []
    if self.is_student():
      roles.append(u"študent")
    if self.is_lecturer():
      roles.append(u"predavatelj")
    if self.is_administrator():
      roles.append(u"referentka")
    if self.is_maintainer():
      roles.append(u"skrbnik")
    
    return roles or ["brez vloge"] 
    
  def get_universityprogram(self):
    """
    Returns current (last) university program object.
    """
    return Enrollment.objects.filter(student__user = self.user).order_by('school_year')[:1][0].university_program or u"Študent ni vpisan v noben program"
  
  def __unicode__(self):
    """
    Returns a string representation of this model.
    """
    if self.student_number is None or len(self.student_number) == 0:    
      return u'(%s) %s %s' % (self.sifra, self.surname, self.name)
    else:
      return u'(%s) %s %s' % (self.student_number, self.surname, self.name)

class EnrollmentSubject(models.Model):
  subject = models.ForeignKey(Subject, verbose_name = u'Predmet')
  enrollment = models.ForeignKey('Enrollment', verbose_name = u'Vpisni list')
  module = models.CharField(u'Modul', max_length = 50)
  
class Enrollment(models.Model):
  """
  A model containing enrollment data for a student.
  """
  def __unicode__(self):
    """
    Returns a string representation of this model.
    """
    return u"%s - %s - %s" % (self.student, self.school_year, self.study_year)  
  
  class Meta:
    ordering = ['school_year__name']
    get_latest_by = 'school_year__name'
  
  student = models.ForeignKey(UserProfile, related_name = 'enrollment_forms')
  university_program = models.ForeignKey(UniversityProgram, null = True, related_name = 'students')
    
  # Enrollment info
  faculty = models.ForeignKey(Faculty)
  
  # When new enrollment is made, subjects are copied to enrollment and student 
  subjects = models.ManyToManyField(Subject, related_name = 'enrollments', through = 'EnrollmentSubject')
    
  modules = models.ManyToManyField(FacultyModule, related_name = 'enrollments')
  
  school_year = models.ForeignKey(SchoolYear, related_name = 'enrollments')
  STUDY_TYPES = (
    ('J', u'visokošolski strokovni'),
    ('K', u'univerzitetni'),
    ('L', u'magistrski'),
    ('N', u'enovit magistrski'),
    ('M', u'doktorski'),
    ('B', u'visokošolski strokovni'),
    ('C', u'univerzitetni'),
    ('E', u'specialistični'),
    ('F', u'magistrski'),
    ('G', u'doktorski'),
    ('H', u'enovit doktorski')
  )
  study_type = models.CharField(u'Vrsta študija', max_length = 1, choices = STUDY_TYPES)
  study_year = models.ForeignKey(FacultyYear, related_name = 'enrollments')
  STUDY_MODES = (
    ('1', 'redni'), 
    ('2', 'izredni')
  )
  study_mode = models.CharField(u'Način študija', max_length = 1, choices = STUDY_MODES)
  DISTANCE_STUDY = (
    ('1','da'),
    ('2','ne')
  )
  distance_study = models.CharField(u'Študij na daljavo', max_length = 1, choices = DISTANCE_STUDY)
  ENROLL_TYPE = (
    ('V1', 'prvi vpis v letnik'),
    ('V2', 'ponavljanje letnika'),
    ('V3', 'nadaljevanje letnika'),
    ('AB', 'absolvent')
  )
  enrollment_type = models.CharField(u'Vrsta vpisa', max_length = 2, choices = ENROLL_TYPE)

